<template>
  <view class="auth-container">
    <!-- 背景装饰（和登录页统一） -->
    <view class="bg-decoration top-left"></view>
    <view class="bg-decoration bottom-right"></view>
    
    <!-- 注册卡片 -->
    <view class="auth-card">
      <view class="auth-header">
        <uni-icons type="left" size="24" color="#666" class="back-btn" @click="goBack"></uni-icons>
        <text class="auth-title">账号注册</text>
        <text class="auth-subtitle">创建健康服务平台账号</text>
      </view>
      
      <!-- 注册表单 -->
      <view class="auth-form">
        <!-- 手机号输入 -->
        <view class="form-item">
          <uni-icons type="phone" size="18" color="#6b7fd7" class="form-icon"></uni-icons>
          <input 
            class="form-input" 
            type="number" 
            placeholder="请输入手机号" 
            v-model="phone"
            maxlength="11"
          >
        </view>
        
        <!-- 验证码输入 -->
        <view class="form-item">
          <uni-icons type="code" size="18" color="#6b7fd7" class="form-icon"></uni-icons>
          <input 
            class="form-input" 
            type="number" 
            placeholder="请输入验证码" 
            v-model="code"
          >
          <text 
            class="code-btn" 
            @click="getCode"
            :style="{ color: isCounting ? '#ccc' : '#6b7fd7' }"
          >
            {{ codeBtnText }}
          </text>
        </view>
        
        <!-- 密码输入 -->
        <view class="form-item">
          <uni-icons type="lock" size="18" color="#6b7fd7" class="form-icon"></uni-icons>
          <input 
            class="form-input" 
            :type="showPwd ? 'text' : 'password'" 
            placeholder="请设置密码（6-16位）" 
            v-model="password"
          >
          <uni-icons 
            :type="showPwd ? 'eye' : 'eye-o'" 
            size="18" 
            color="#666"
            @click="showPwd = !showPwd"
          ></uni-icons>
        </view>
        
        <!-- 确认密码 -->
        <view class="form-item">
          <uni-icons type="lock" size="18" color="#6b7fd7" class="form-icon"></uni-icons>
          <input 
            class="form-input" 
            :type="showConfirmPwd ? 'text' : 'password'" 
            placeholder="请确认密码" 
            v-model="confirmPwd"
          >
          <uni-icons 
            :type="showConfirmPwd ? 'eye' : 'eye-o'" 
            size="18" 
            color="#666"
            @click="showConfirmPwd = !showConfirmPwd"
          ></uni-icons>
        </view>
        
        <!-- 协议勾选 -->
        <view class="agreement">
          <uni-checkbox v-model="agree" color="#6b7fd7"></uni-checkbox>
          <text class="agreement-text">我已阅读并同意《用户协议》</text>
        </view>
      </view>
      
      <!-- 注册按钮 -->
      <view class="auth-btn" @click="doRegister">
        注册
      </view>
      
      <!-- 登录入口 -->
      <view class="have-account">
        <text class="account-text">已有账号？</text>
        <text class="login-link" @click="goLogin">立即登录</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onUnmounted } from 'vue';

// 1. 核心表单数据（只保留必要字段）
const phone = ref(''); // 手机号
const code = ref(''); // 验证码
const password = ref(''); // 密码
const confirmPwd = ref(''); // 确认密码
const showPwd = ref(false); // 密码是否可见
const showConfirmPwd = ref(false); // 确认密码是否可见
const agree = ref(true); // 同意协议

// 2. 验证码相关（简化倒计时）
const codeBtnText = ref('获取验证码');
const isCounting = ref(false);
let codeTimer = null;

// 3. 获取验证码
const getCode = () => {
  // 简单手机号验证
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    return;
  }
  
  // 60秒倒计时
  let count = 60;
  isCounting.value = true;
  codeBtnText.value = `${count}s后重发`;
  
  codeTimer = setInterval(() => {
    count--;
    if (count <= 0) {
      clearInterval(codeTimer);
      isCounting.value = false;
      codeBtnText.value = '获取验证码';
    } else {
      codeBtnText.value = `${count}s后重发`;
    }
  }, 1000);
  
  // 模拟发送验证码
  uni.showToast({ title: '验证码已发送', icon: 'none' });
};

// 4. 执行注册
const doRegister = () => {
  // 分步验证
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    return;
  }
  if (!code.value || code.value.length !== 6) {
    uni.showToast({ title: '请输入6位验证码', icon: 'none' });
    return;
  }
  if (password.value.length < 6 || password.value.length > 16) {
    uni.showToast({ title: '密码需6-16位', icon: 'none' });
    return;
  }
  if (password.value !== confirmPwd.value) {
    uni.showToast({ title: '两次密码不一致', icon: 'none' });
    return;
  }
  if (!agree.value) {
    uni.showToast({ title: '请同意用户协议', icon: 'none' });
    return;
  }
  
  // 模拟注册
  uni.showLoading({ title: '注册中...' });
  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({ title: '注册成功', icon: 'success' });
    // 注册成功跳登录页
    setTimeout(() => uni.navigateTo({ url: '/pages/login' }), 1000);
  }, 1500);
};

// 5. 页面跳转
const goBack = () => {
  uni.navigateBack({ delta: 1 });
};
const goLogin = () => {
  uni.navigateTo({ url: '/pages/login' });
};

// 6. 页面卸载清除定时器
onUnmounted(() => {
  if (codeTimer) clearInterval(codeTimer);
});
</script>

<style scoped>
/* 和登录页统一的基础样式，只保留核心 */
.auth-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bg-decoration {
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  filter: blur(100px);
  z-index: 0;
}

.top-left {
  top: -100px;
  left: -100px;
  background: linear-gradient(135deg, #6b7fd7, rgba(107, 127, 215, 0.2));
}

.bottom-right {
  bottom: -100px;
  right: -100px;
  background: linear-gradient(135deg, #8a5cf6, rgba(138, 92, 246, 0.2));
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 16px;
  padding: 30px 25px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.08);
  position: relative;
  z-index: 1;
}

.auth-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
}

.auth-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  display: block;
}

.auth-subtitle {
  font-size: 14px;
  color: #666;
}

.auth-form {
  margin-bottom: 25px;
}

.form-item {
  display: flex;
  align-items: center;
  background: #f5f7fa;
  border-radius: 8px;
  padding: 0 15px;
  height: 50px;
  margin-bottom: 15px;
}

.form-icon {
  margin-right: 10px;
}

.form-input {
  flex: 1;
  height: 100%;
  background: transparent;
  font-size: 14px;
  color: #333;
}

.code-btn {
  font-size: 14px;
  cursor: pointer;
}

/* 协议样式 */
.agreement {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
  margin: 5px 0 20px;
}

.agreement-text {
  margin-left: 8px;
}

/* 注册按钮 */
.auth-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #6b7fd7, #8a5cf6);
  color: #fff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 25px;
}

/* 已有账号 */
.have-account {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.login-link {
  color: #6b7fd7;
  margin-left: 5px;
  cursor: pointer;
}
</style>